package test2;

import java.io.*;
import java.net.*;

public class HttpClient {
    public static void main(String[] args) {
        try {
            // 创建一个URL对象，指向我们要请求的服务器地址
            URL url = new URL("http://localhost:8080/hello?name=John");

            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为 GET
            connection.setRequestMethod("GET");

            // 获取响应状态码
            int statusCode = connection.getResponseCode();
            System.out.println("HTTP 状态码: " + statusCode);

            // 读取服务器返回的响应内容
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 输出服务器响应
            System.out.println("响应内容: " + response.toString());

            // 关闭连接
            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
